//
//  SouSuoDetailViewController.m
//  livingDesign
//
//  Created by dlios on 15-1-31.
//  Copyright (c) 2015年 怡居. All rights reserved.
//

#import "SouSuoDetailViewController.h"
#import "NewestMiddleView.h"
#import "NewestMiddleCollectionViewCell.h"
#import "ASIHTTPRequest.h"
#import "Newest.h"


@interface SouSuoDetailViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property(nonatomic, retain) NewestMiddleView *newestV;
@property (nonatomic, retain)Newest *sousuoDataNewest;
@property (nonatomic, retain)NSMutableArray *sousuoArr;
@property (nonatomic, retain)NSString *strURL;   // 接口字符串
@property (nonatomic, retain)NSString *bossPath;    // 缓存路径
@property (nonatomic, retain) NSURL *url;


@end

@implementation SouSuoDetailViewController
- (void)dealloc
{
    [_newestV release];
    [_sousuoDataNewest release];
    [_sousuoArr release];
    [_sousuoStr release];
    [_strURL release];
    [_bossPath release];
    [_url release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSLog(@"%@", self.sousuoStr);
    self.view.backgroundColor = [UIColor whiteColor];

    self.newestV = [[NewestMiddleView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:self.newestV];
    self.newestV.collectView.delegate = self;
    self.newestV.collectView.dataSource = self;
    [self.newestV release];
    
//    // 初始化
    self.sousuoArr = [NSMutableArray array];

    // 使用ASI网络请求
    NSString *pathStr = @"http://rm.app.diaoyuweng.com/json_rm.php?action=list&ver=4&page=0&category=jiaju&category2=&pageSize=10&appCookie=channel_AppStore%20version_4.1.2%20oid_0f607264fc6318a92b9e13c65db7cd3c%20package_com.aslibra.rm.jiaju%20platform_iphone%20ver_8.1.2%20jb_0&keyword=%E5%AE%B6%E5%B1%85%E6%9C%8D%E9%A5%B0";
    self.strURL = [pathStr stringByReplacingOccurrencesOfString:@"%E5%AE%B6%E5%B1%85%E6%9C%8D%E9%A5%B0" withString:self.sousuoStr];
    self.url = [NSURL URLWithString:self.strURL];
    
    // 获取本地路径
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    self.bossPath = [NSString stringWithFormat:@"%@/%ld",docPath, [self.strURL hash]];
    NSLog(@"***********^_^*********%@", self.bossPath);
//
//    // 判断是否为第一次搜索  选择是否网络请求
//    if (self.tag == 1) {
//        [self requestByASI];
//    }
//    else
//    {
//        NSLog(@"^^^^^^^^^^^^^^^^^^阿偶………………………………");
//        NSData *data = [NSKeyedUnarchiver unarchiveObjectWithFile:self.bossPath];
//        NSLog(@"%@", self.bossPath);
//        if (data) {
//            NSLog(@"!!!!!!!!!!!!!!!! 没有取到本地数据！！！！！！");
//            [self analysisData:data];
//        }
//    }
    [self gainData];

}
// 网络请求
- (void)requestByASIWithURL:(NSURL *)url
{
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    request.delegate = self;
    [request startAsynchronous];
    
}
//ASI的协议方法

- (void)requestFinished:(ASIHTTPRequest *)request
{
    // 解析数据
    NSData *data = [request responseData];
    // 将数据放到路径下
    [NSKeyedArchiver archiveRootObject:data toFile:self.bossPath];
    
    [self analysisData:data];
    // 请求完之后将数据存到本地  以便第二次获取
   
    
}

- (void)requestFailed:(ASIHTTPRequest *)request
{
    NSLog(@"请求失败");
}
// 数据解析
- (void)analysisData:(NSData *)data
{
    //数组初始化
    self.sousuoArr = [NSMutableArray array];
    NSDictionary *tempDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    NSArray *tempArr = [tempDic objectForKey:@"data"];
    for (NSDictionary *dataDic in tempArr) {
        self.sousuoDataNewest = [[Newest alloc] init];
        [self.sousuoDataNewest setValuesForKeysWithDictionary:dataDic];
        [self.sousuoArr addObject:self.sousuoDataNewest];
        [self.sousuoDataNewest release];
    }
    //    self.newestMiddle.arr = self.newestArr;
    NSLog(@"sousuo--------ok");
    NSLog(@"%@", self.sousuoArr);
    [self.newestV.collectView reloadData];
}
- (void)gainData
{
    // 判断是否为第一次搜索  选择是否网络请求
    if (self.tag == 1) {
        [self requestByASIWithURL:self.url];
    }
    else
    {
        NSLog(@"^^^^^^^^^^^^^^^^^^阿偶………………………………");
        NSData *data = [NSKeyedUnarchiver unarchiveObjectWithFile:self.bossPath];
        NSLog(@"如果可以走这个吧——————————————————%@", self.bossPath);
        if (data) {
            NSLog(@"!!!!!!!!!!!!!!!! 没有取到本地数据！！！！！！");
            [self analysisData:data];
        }
    }
}
//collectionView的协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.sousuoArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NewestMiddleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.newest = [self.sousuoArr objectAtIndex:indexPath.item];
    //    cell.backgroundColor = [UIColor yellowColor];
    return cell;
}



//

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
